/**
 * Created by Jimmy on 2018/12/11.
 */
<template>
  <div class="output-distribution">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/panel/project/quality' }">质检审批</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form v-model="form" ref="form" class="fr" :inline="true">
      <el-form-item label="">
        <el-checkbox v-model="form.tobeApprove" @change="tobeApprove">待审批</el-checkbox>
        <!--<el-button type="primary" size="small" @click="loadList(currentPage)">筛选</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      border
      :stripe= 'true'
      class=""
      style="width:100%">
      <el-table-column type="index" label="序号" align="center" width="50"></el-table-column>
      <el-table-column prop="SubmitDate" label="送审时间" align="center" width="100"></el-table-column>
      <el-table-column prop="ProjectNumber" label="项目编号" align="center" width="130"></el-table-column>
      <el-table-column label="项目名称" align="center" class="link-txt">
        <template slot-scope="scope">
          <el-button type="text" @click="detailCheck(scope.row.ProjectID)">{{scope.row.ProjectName}}</el-button>
        </template>
      </el-table-column>
      <!--<el-table-column prop="ProjectTypeName" label="项目类型" align="center" width="100"></el-table-column>-->
      <el-table-column prop="PmName" label="负责人" align="center" width="100"></el-table-column>
      <el-table-column prop="Qcoe" label="质检系数" align="center" width="100"></el-table-column>
      <el-table-column prop="Qcresoult" label="审批结果" align="center" width="100"></el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.Qcresoult == '--'">
            <el-button type="danger" size="small" @click="approve(false,scope.row.ID,scope.row.ProjectID,scope.row.Qcoe)" plain>拒绝</el-button>
            <el-button type="primary" size="small" @click="approve(true,scope.row.ID,scope.row.ProjectID,scope.row.Qcoe)">同意</el-button>
          </div>
          <div v-else>
            已审批
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      layout="total, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  data () {
    return {
      total:0,
      currentPage:1,
      form:{
        tobeApprove: true,
      },
      tableData:[]
    }
  },
  created: function(){
    let user = JSON.parse(window.localStorage.getItem('access-user'))
    this.token = user.Token
    this.getQcs(this.currentPage)
  },
  methods:{
    detailCheck(ID){
      let routeData = this.$router.resolve({
        path: '/panel/project/detail',
        query: {
          id: ID
        }
      })
      window.open(routeData.href, '_blank')
    },
    getQcs(page){
      let args = {
        token: this.token,
        isapproved: !this.form.tobeApprove,
        page:page,
        limit:10,
      }
//      console.log(args)
      this.$http.post(this.domain + '/approve/list',
        args
      ).then((response) => {
        if (response.data.Success) {
          this.tableData = []
//          console.log(response.data)
          if (response.data.Data) {
            response.data.Data.forEach((val) => {
              var row = {
                ID: val.qc.ID,
                SubmitDate: val.qc.SubmitDate,
                ProjectID:val.project.ID,
                ProjectNumber:val.project.ProjectNumber,
                ProjectName:val.project.ProjectName,
                Qcoe:val.qc.Qcoe,
                PmName:val.pm.RealName,
              }
              if (val.qc.IsApproved) {
                row.Qcresoult = val.qc.ApproveResult ? "同意":"拒绝"
              } else {
                row.Qcresoult = "--"
              }
              this.tableData.push(row)
            })
          }
          this.$message.success(response.data.Msg)
        } else {
          this.$message.error(response.data.Msg)
        }
      }).catch(function (response) {
        this.$message.error(response.data)
      })
    },
    approve(val,id,pjid,qcoe){
      console.log(id)
      let msg = val ? "确定同意吗？":"确定拒绝吗？"
      this.$confirm(msg, '提示', {
        confirmButtonClass: 'el-button--warning'
      }).then(() => {
        let arg = {
          token:this.token,
          approveresult:val,
          id:id,
          projectid:pjid,
          qcoe:qcoe
        }
        console.log(arg)
        this.$http.post(this.domain + '/approve/qcoe',
          arg
        ).then((response) => {
          if (response.data.Success) {
            this.$message.success(response.data.Msg)
            this.getQcs(this.currentPage)
          } else {
            this.$message.error(response.data.Msg)
          }
        }).catch((response) => {
          this.$message.error(response.data)
        })
      }).catch(() => {})
    },
    tobeApprove() {
      this.getQcs(this.currentPage)
    },
    handleCurrentChange(val){
      this.currentPage = val;
      this.getQcs(this.currentPage)
    },
  }
}
</script>

<style scoped>
</style>
